package com.lzx.iteam.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import com.lzx.iteam.ImagePagerActivity;
import com.lzx.iteam.R;
import com.lzx.iteam.selectimage.Bimp;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.ImageLoaderInterface;
import com.lzx.iteam.util.StringUtil;

import java.util.ArrayList;

/**
 * Created by Megan on 15/10/9.
 */
public class EventImageAdapter extends BaseAdapter {
    private Context mContext;
    private String[] mImageDatas;
    private ArrayList<String> mOriImages;
    private String mThumeImages;
    private ViewHolder holder = null;
    private String mTalkId;
    private String mEventId;
    private boolean mIsNew;

    /**
     * 事件列表  和 普通事件详情 + 外勤签到
     * @param context
     * @param mImageDatas
     * @param mOriImages
     * @param mThumeImages
     * @param mEventId
     */
    public EventImageAdapter(Context context, String[] mImageDatas,ArrayList<String> mOriImages,String mThumeImages,String mEventId) {
        this.mContext = context;
        this.mImageDatas = mImageDatas;
        this.mOriImages = mOriImages;
        this.mThumeImages = mThumeImages;
        this.mEventId = mEventId;
    }

    /**
     * 事件新回复
     * @param context
     * @param mImageDatas
     * @param mOriImages
     * @param mThumeImages
     * @param isNew
     */
    public EventImageAdapter(Context context, String[] mImageDatas,ArrayList<String> mOriImages,String mThumeImages,boolean isNew) {
        this.mContext = context;
        this.mImageDatas = mImageDatas;
        this.mOriImages = mOriImages;
        this.mThumeImages = mThumeImages;
        this.mIsNew = isNew;
    }

    /**
     * 新建事件  和  事件详情(除普通事件)
     * @param context
     * @param mEventId
     * @param mImageDatas
     */
    public EventImageAdapter(Context context,String mEventId,String[] mImageDatas) {
        this.mContext = context;
        this.mImageDatas = mImageDatas;
        this.mEventId = mEventId;

    }

    /**
     * 新建人脉圈
     * @param context
     * @param mImageDatas
     * @param talkId
     */
    public EventImageAdapter(Context context, String[] mImageDatas,String talkId) {
        this.mContext = context;
        this.mImageDatas = mImageDatas;
        this.mTalkId = talkId;
    }

    public EventImageAdapter(Context context, String[] mImageDatas,ArrayList<String> mOriImages,String mThumeImages) {
        this.mContext = context;
        this.mImageDatas = mImageDatas;
        this.mOriImages = mOriImages;
        this.mThumeImages = mThumeImages;
    }

    @Override
    public int getCount() {
        return mImageDatas.length;
    }

    @Override
    public Object getItem(int position) {
        return mImageDatas[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

       String imageData = mImageDatas[position];
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.event_image_item, null);
            holder.img = (ImageView) convertView.findViewById(R.id.event_item_image);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String path = "";
        // TODO 如果是新建的 有图片，第一次先显示本地的图片 ，加载网址然并卵
        if (!StringUtil.isEmpty(Constants.NEW_TALK_ID) && !StringUtil.isEmpty(mTalkId) && mTalkId.equals(Constants.NEW_TALK_ID)){
            if (Bimp.mPictureData.size() -1 >= position){
                path = "file://" + Bimp.mPictureData.get(position);
            }
            ImageLoaderInterface.imageLoader.displayImage(path, holder.img, ImageLoaderInterface.optionEventImage);
            Log.d("talk_list_send_da",Bimp.mPictureData.size() + "  none    " + path); // 新建人脉圈
        }else if (!StringUtil.isEmpty(Constants.NEW_EVENT_ID) && !StringUtil.isEmpty(mEventId) && mEventId.equals(Constants.NEW_EVENT_ID)){
            if (Bimp.mPictureData.size() -1 >= position){
                path = "file://" + Bimp.mPictureData.get(position);
            }
            ImageLoaderInterface.imageLoader.displayImage(path, holder.img, ImageLoaderInterface.optionEventImage);
            Log.d("event_list","none"); // 新建事件
        } else if (!StringUtil.isEmpty(Constants.NEW_LEGWORK_ID) && !StringUtil.isEmpty(mEventId) && mEventId.equals(Constants.NEW_LEGWORK_ID)){
            if (Bimp.mPictureData.size() -1 >= position){
                path = "file://" + Bimp.mPictureData.get(position);
            }
            ImageLoaderInterface.imageLoader.displayImage(path, holder.img, ImageLoaderInterface.optionEventImage);
            Log.d("event_list","none"); // 新建事件
        }else if (mIsNew){
            if (Bimp.mPictureData.size() -1 >= position){
                path = "file://" + Bimp.mPictureData.get(position);
            }
            ImageLoaderInterface.imageLoader.displayImage(path, holder.img, ImageLoaderInterface.optionEventImage);
            Log.d("event_new_reply","none"); // 事件新回复
        }else{
            ImageLoaderInterface.imageLoader.displayImage(imageData, holder.img, ImageLoaderInterface.optionEventImage);
            Log.d("talk_list","show");
        }
        if (mOriImages != null){
            holder.img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(mContext, ImagePagerActivity.class);
                    intent.putExtra(Constants.IMAGE_ORIGIN, Constants.IMAGE_ORIGIN_URL_ARRAY);
                    intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, mOriImages);
                    intent.putExtra(ImagePagerActivity.EXTRA_THUME_IMAGES, mThumeImages);
                    intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
                    mContext.startActivity(intent);
                }
            });
        }

        return convertView;
    }

    private class ViewHolder {
        ImageView img;
    }
}
